package com.wonders.frame.repository;

import com.wonders.frame.entity.AuthDic;
import com.wonders.frame.repository.core.CoreRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AuthDicRepository extends CoreRepository<AuthDic> {

    @Query("select d from AuthDic d where removed = 0 and dicType=:dicType and(code=:code or name=:name )")
    List<AuthDic> checkUniqueAdd(@Param("code") String code,@Param("name") String name, @Param("dicType") String dicType);

    @Query(nativeQuery = true,value = "SELECT ad.dic_type,ad.code,ad.name\n" +
            "FROM auth_dic ad,auth_dic_type adt\n" +
            "where adt.dic_type='system' AND ad.removed=0 AND adt.code=ad.dic_type order by ad.dic_type,ad.code asc")
    List getAllDic();

    @Query("select d from AuthDic d where dicType=:dicType and removed = 0 order by seq")
    List<AuthDic> getByDicType(@Param("dicType") String dicType);

    @Query("select d.id from AuthDic d where dicType=:dicType and removed = 0 order by seq")
    List<String> getIdsByDicType(@Param("dicType") String dicType);

    @Query("select d.id from AuthDic d where  removed = 0 order by seq")
    List<String> getAllIds();

    @Query("select d from AuthDic d where removed = 0 order by d.seq")
    List<AuthDic> getAllAuthDic();

    AuthDic getByCodeAndAndDicTypeAndRemoved(String code, String dicType, Integer removed);

    @Query("select d.id from AuthDic d ,AuthDicType dt where dt.id=?1 and d.removed = 0 and d.dicType=dt.code order by d.seq")
    List<String> findByDicTypeId(String dicTypeId);


    List<AuthDic> findByCodeAndDicTypeAndRemoved(String code,String dicType,Integer removed);

    List<AuthDic> findByNameAndDicTypeAndRemoved(String name,String dicType,Integer removed);
}
